package routes

import (
	"github.com/garyburd/redigo/redis"
	"github.com/martini-contrib/render"
	"log"
	"message"
	"net/http"
	"rhelper"
)

func GetTestEntryView(r render.Render) {
	r.HTML(200, "testEntry", TmplValue{Title: "Test function set"})
}

func GetTestSendMsg2AccountView(r render.Render) {
	r.HTML(200, "sm2aEdit", TmplValue{Title: "Send mesage to account"})
}

type Message2AccountEditValue struct {
	To       string `form:"textinput-to" json:"to,omitempty"`
	From     string `form:"textinput-from" json:"from,omitempty"`
	Type     string `form:"textinput-type" json:"type,omitempty"`
	Title    string `form:"textinput-title" json:"title,omitempty"`
	Abstract string `form:"textinput-abstract" json:"abstract,omitempty"`
	Detail   string `form:"textinput-detail" json:"detail,omitempty"`
}

func CheckMsgAccountTo(req *http.Request) string {
	var account = req.URL.Query().Get("textinput-to")
	if account == "" {
		return "account must specify"
	}

	conn := rhelper.Pool().Get()
	defer conn.Close()

	p, err := redis.String(conn.Do("HGET", rhelper.AccountHash+account, "password"))
	if err != nil {
		log.Println(err)
	}

	if p != "" {
		return "true"
	}

	return "account not exist"
}

func SendMessage2Account(msgValue Message2AccountEditValue) string {
	var msg message.Message = message.Message{}
	msg.Type = msgValue.Type
	msg.To = msgValue.To
	msg.Title = msgValue.Title
	msg.From = msgValue.Title
	msg.Abstract = msgValue.Abstract
	msg.Detail = msgValue.Detail

	message.SendMessage(msg)
	return "ok"
}
